//
//  AEPointsAcquireDailyView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/16.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEPointsAcquireDailyView.h"
#import "AE_Solar-Swift.h"
#import "AEPointsSingleSignView.h"
#import "AEPointsAcquireViewModel.h"
#import "AEPointsSingleSignBigView.h"

@interface AEPointsAcquireDailyView ()

@property (nonatomic, strong) AEPointsAcquireViewModel *viewModel;

//ic_points_daily_title

@property (nonatomic, strong) UIImageView *dailyTitleImgView;

@property (nonatomic, strong) UILabel *remarkLabel;

/// 点击签到
@property (nonatomic, strong) UIButton *btnSign;

/// 背景图
@property (nonatomic, strong) UIImageView *signInBgImgView;

@end

@implementation AEPointsAcquireDailyView

- (void)initWithSubView {
    [super initWithSubView];
    
    UIImage *iamge = [UIImage imageNamed:@"ic_points_aquire_daily"];
    self.layer.contents = (__bridge id)iamge.CGImage;
    self.layer.contentsScale = [UIScreen mainScreen].scale;
    // 填充方式
//    self.layer.contentsGravity = kCAGravityResizeAspect;
    
    [self initWithDailyTitleImgView];
    
    [self initWithRemarkLabel];
    
    [self initWithBtnSign];
    
    [self initWithSignInBgImgView];
}

/// 每日签到福利 文字图片
- (void)initWithDailyTitleImgView {
    if (!_dailyTitleImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_points_daily_title"];
        CGRect frame = CGRectMake(7, 6, image.size.width, image.size.height);
        _dailyTitleImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

- (void)initWithRemarkLabel {
    if (!_remarkLabel) {
        CGRect frame = CGRectMake(_dailyTitleImgView.left, self.dailyTitleImgView.bottom, self.width - _dailyTitleImgView.left * 2, 13);
        NSString *title = [AEStringExten localizedValueWithKey:@"签到越多，奖励越多哦~  "];
        UIFont *font = [UIFont systemFontOfSize:9.5 weight:UIFontWeightBold];
        _remarkLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#303030"];
            label.text = title;
        }];
    }
}

// ic_click_sign_in_points
- (void)initWithBtnSign {
    if (!_btnSign) {
        NSString *title = [AEStringExten localizedValueWithKey:@"点击签到"];
        UIImage *image = [UIImage imageNamed:@"ic_click_sign_in_points"];
        UIFont *font = [UIFont systemFontOfSize:21 weight:UIFontWeightBold];
        CGRect frame = CGRectMake((self.width - image.size.width) * 0.5, self.height - image.size.height - 10, image.size.width, image.size.height);
        _btnSign = [UIButton createWithButton:self frame:frame font:font target:self action:@selector(actionWithSign:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setBackgroundImage:image forState:UIControlStateNormal];
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
        }];
    }
}

- (void)initWithSignInBgImgView {
    if (!_signInBgImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_sign_in_bg"];
        CGRect frame = CGRectMake(0, self.remarkLabel.bottom - 4, self.width, self.btnSign.top - self.remarkLabel.bottom - 3 - 0 + 12);
//        image = [UIImage imageWithImage:image scaledToSize:frame.size];
        _signInBgImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
        }];
    }
}


- (void)initWithSigns {
    NSInteger count = self.viewModel.signList.count - 1;
    float topSpace = 61;
    float leftSpace = 23;
    float width = kAEPointsSingleSignViewWidth;
    float height = kAEPointsSingleSignViewHeight;
    float midSpace = (self.width - leftSpace * 2 - width * 4) / 3.0;
    CGRect frame = CGRectMake(leftSpace, topSpace, width, height);
    AEPointsSingleSignView *preView = nil;
    for (int i=0; i<count; i++) {
        TYZCommonCellModel *model = self.viewModel.signList[i];
        if (i % 4 == 0) {
            if (preView) {
                frame.origin.x = leftSpace;
                frame.origin.y = preView.bottom + 7.5;
            }
        } else {
            frame.origin.x = preView.right + midSpace;
        }
        AEPointsSingleSignView *singView = [[AEPointsSingleSignView alloc] initWithFrame:frame];
        [singView updateWithViewData:model];
        [self addSubview:singView];
        [self.viewList addObject:singView];
        preView = singView;
    }
    
    // 第7天
    CGRect lastFrame = CGRectMake(preView.right + midSpace, preView.top, width * 2 + midSpace, height);
    AEPointsSingleSignBigView *lastView = [[AEPointsSingleSignBigView alloc] initWithFrame:lastFrame];
    [lastView updateWithViewData:self.viewModel.signList.lastObject];
    [self addSubview:lastView];
    [self.viewList addObject:lastView];
}

/// 点击签到
/// - Parameter sender: sender description
- (void)actionWithSign:(id)sender {
    if (self.touchSignInBlock) {
        self.touchSignInBlock();
    }
}

- (void)updateWithViewData:(id)data {
    self.viewModel = data;
    
    [self initWithSigns];
}

@end
